export function addScrollEventPrev(selector: any, li: any) {
  const cl = document.querySelector(selector)
  const ul = cl.querySelector('.no-scrollbar')
  const liWidth = ul.querySelector(li).offsetWidth
  ul.scrollBy({ left: -liWidth, behavior: 'smooth' })
}

export function addScrollEventNext(selector: any, li: any) {
  const cl = document.querySelector(selector)
  const ul = cl.querySelector('.no-scrollbar')
  const liWidth = ul.querySelector(li).offsetWidth
  ul.scrollBy({ left: liWidth, behavior: 'smooth' })
}

export function timeAgo(dateTimeStamp: any) {
  let minute: number = 1000 * 60
  let hour: number = minute * 60
  let day: number = hour * 24
  let week: number = day * 7
  let month: number = day * 30
  let now: number = new Date().getTime()
  let diffValue: number = now - dateTimeStamp
  if (diffValue < 0) {
    return
  }
  let minC: number = diffValue / minute
  let hourC: number = diffValue / hour
  let dayC: number = diffValue / day
  let weekC: number = diffValue / week
  let monthC: number = diffValue / month
  let result
  if (monthC >= 1) {
    result = '' + parseInt(String(monthC)) + '月前'
  } else if (weekC >= 1) {
    result = '' + parseInt(String(weekC)) + '周前'
  } else if (dayC >= 1) {
    result = '' + parseInt(String(dayC)) + '天前'
  } else if (hourC >= 1) {
    result = '' + parseInt(String(hourC)) + '小时前'
  } else if (minC >= 1) {
    result = '' + parseInt(String(minC)) + '分钟前'
  } else {
    result = '刚刚'
  }
  return result
}

export function timeDifference(nowTime: any) {
  let timeOld = new Date().getTime() - new Date(nowTime).getTime()
  let msPerDay = 24 * 60 * 60 * 1000
  return Math.floor(timeOld / msPerDay)
}

export function initFavicon(faviconUrl: string) {
  if (!faviconUrl) {
    return
  }
  // 获取 head 标签
  let head = document.getElementsByTagName('head')[0]
  // 获取当前 favicon 元素
  let favicon = document.querySelector('link[rel*=\'icon\']') || document.createElement('link')
  // Cast favicon to HTMLLinkElement
  let faviconLink = favicon as HTMLLinkElement

  faviconLink.type = 'image/x-icon'
  faviconLink.rel = 'shortcut icon'

  // 设置新的 favicon 地址
  faviconLink.href = faviconUrl

  // 如果当前 head 标签中不存在 favicon 元素，则将新的 favicon 添加到 head 标签中
  if (!document.querySelector('link[rel*=\'icon\']')) {
    head.appendChild(faviconLink)
  }
}

export function timeInfoComputed(time: any, inc: number) {
  let year = new Date(time).getFullYear()
  let month = new Date(time).getMonth() + 1
  let day = new Date(time).getDate()
  return (year + inc) + '年' + month + '月' + day + '日'
}

export function description(description:string){
  let metaTag = document.querySelector('head > meta[name="description"]');
  if (metaTag) {
    // 如果存在Meta标签则直接修改其content属性值为指定的内容
    metaTag.setAttribute("content", description);
  } else {
    // 如果不存在Meta标签则创建一个新的Meta标签并添加到头部
    let newMetaTag = document.createElement("meta");
    newMetaTag.setAttribute("name", "description");
    newMetaTag.setAttribute("content", description);
    document.getElementsByTagName("head")[0].appendChild(newMetaTag);
  }
}
export function keyWord(keywords:string){
  let metaTag = document.querySelector('head > meta[name="keywords"]');
  if (metaTag) {
    // 如果存在Meta标签则直接修改其content属性值为指定的内容
    metaTag.setAttribute("content", keywords);
  } else {
    // 如果不存在Meta标签则创建一个新的Meta标签并添加到头部
    let newMetaTag = document.createElement("meta");
    newMetaTag.setAttribute("name", "keywords");
    newMetaTag.setAttribute("content", keywords);
    document.getElementsByTagName("head")[0].appendChild(newMetaTag);
  }
}
export function insertHtmlTag(htmlTag:string){
  if (htmlTag != null){
    let newTag = document.createElement("style");
    let cssText = document.createTextNode(htmlTag);
    newTag.setAttribute("type", "text/css");
    newTag.appendChild(cssText);
    document.getElementsByTagName("head")[0].appendChild(newTag);
  }
}

export function insertEndHtmlTag(htmlTag:string){
  if (htmlTag != null){
    let tags = htmlTag.split("\n")
    let newTag = document.createElement("style");
    newTag.innerHTML= htmlTag;
    // @ts-ignore
    document.getElementById("app").appendChild(newTag);
  }
}
/** 写入网页标题 */
export function setDocumentTitle(title: string): void {
  document.title = title;
}
